package dao.search;

import bean.search.PersonSearch;
import util.C3p0Util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


/**
 * 个人信息查询数据交互实现类
 **/
public class PersonSearchDao {
    public List<PersonSearch> personSearch(int sNumber){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<PersonSearch> pps = null;

        String sql = "select s_number,s_name,s_sex,s_c_number,s_tel,r_number from t_student\n" +
                "inner join t_room on t_student.s_r_number = t_room.r_number where s_number = ?;";

        try {
            conn = C3p0Util.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setInt(1,sNumber);
            rs = ps.executeQuery();
            while(rs.next()) {
                if (pps == null) {
                    pps = new ArrayList<PersonSearch>();
                }
                PersonSearch personSearch = new PersonSearch();
                personSearch.setsNumber(rs.getInt("s_number"));
                personSearch.setsName(rs.getString("s_name"));
                personSearch.setsSex(rs.getString("s_sex"));
                personSearch.setScNumber(rs.getInt("s_c_number"));
                personSearch.setsTel(rs.getInt("s_tel"));
                personSearch.setrNumber(rs.getInt("r_number"));


                pps.add(personSearch);

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return pps;
    }
}
